package com.module.user.ui.modifyNikeName

import android.graphics.Color
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.common.app.base.AppActivity
import com.common.app.utls.HeadTitleUtils
import com.common.base.app.extras.addRoundCornersWithBorder
import com.common.base.app.extras.no
import com.common.base.app.extras.setOnClickListener
import com.common.base.app.extras.setViIn
import com.common.base.utils.BitmapUtils
import com.common.base.utils.StatusBarUtils
import com.common.base.utils.ViewUtils
import com.module.user.R
import com.module.user.databinding.UserActivityModifyNikeNameBinding
import com.module.user.ui.me.MeViewModel


/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/7/4
 *
 * 描述：修改昵称页
 *
 * 修订历史：
 *
 */
class ModifyNikeNameActivity : AppActivity<UserActivityModifyNikeNameBinding, MeViewModel>(), View.OnClickListener {

    @Autowired(name = "mValue")
    @JvmField
    var mValue: String? = null

    var headTitleUtils: HeadTitleUtils? = null

    override fun getViewModel(): Class<MeViewModel> = MeViewModel::class.java

    override fun getViewContentBinding() =
        UserActivityModifyNikeNameBinding.inflate(layoutInflater)

    override fun isShowToolBar(): Boolean = false
    override fun isStatusBarTranslate(): Boolean = true

    override fun initViews() {
        super.initViews()
        StatusBarUtils.setStatusBarHeight(mViewContentBinding.statusView)
        StatusBarUtils.setStatusBarForegroundColor(this, true)

        headTitleUtils = HeadTitleUtils(this)
        headTitleUtils?.setTitle(resources.getString(R.string.ac_modify_nike_name_title))
        headTitleUtils?.setLineStyle(10)

    }

    override fun initEvents() {
        super.initEvents()

        headTitleUtils?.setRightTextActionCallBack(object :
            HeadTitleUtils.RightTextActionListener {
            override fun rightTextAction(v: View?) {
                val intent = intent
                if (!TextUtils.isEmpty(mViewContentBinding.changeEdt.text.toString().trim())) {
                    intent.putExtra("nikeName", mViewContentBinding.changeEdt.text.toString().trim())
                }
                setResult(RESULT_OK, intent)
                finish()
            }
        })

        mViewContentBinding.changeEdt.addTextChangedListener(object : TextWatcher {
            override fun onTextChanged(
                s: CharSequence, start: Int, before: Int,
                count: Int
            ) {
//                    //s:变化后的所有字符
//                    Log.i("Seachal:","变化:"+s+";"+start+";"+before+";"+count);
            }

            override fun beforeTextChanged(
                s: CharSequence, start: Int, count: Int,
                after: Int
            ) {
//                    //s:变化前的所有字符； start:字符开始的位置； count:变化前的总字节数；after:变化后的字节数
//                    Log.i("Seachal:","变化前:"+s+";"+start+";"+count+";"+after);
            }

            override fun afterTextChanged(s: Editable) {
//                    //S：变化后的所有字符；start：字符起始的位置；before: 变化之前的总字节数；count:变化后的字节数
//                    Log.i("Seachal:","变化后:"+s+";");

                //如果改变了，并且文本长度>0
                if (!TextUtils.isEmpty(s.toString())) {
                    mViewContentBinding.deleteBtn.visibility = View.VISIBLE
                    mViewContentBinding.btnSave.isEnabled = true
                } else {
                    mViewContentBinding.deleteBtn.visibility = View.GONE
                    mViewContentBinding.btnSave.isEnabled = false
                }
            }
        })

        mViewContentBinding.deleteBtn.setOnClickListener(onClick = this)
        mViewContentBinding.btnSave.setOnClickListener(onClick = this)

    }

    override fun lazyInitData() {
        super.lazyInitData()

        mValue.isNullOrEmpty().no {
            mViewContentBinding.changeEdt.hint = mValue
        }

    }

    override fun onClick(v: View?) {
        when (v) {
            mViewContentBinding.deleteBtn -> {
                mViewContentBinding.changeEdt.setText("")
                mViewContentBinding.deleteBtn.visibility = View.GONE
            }
            mViewContentBinding.btnSave -> {
                if (!TextUtils.isEmpty(mViewContentBinding.changeEdt.text.toString().trim())) {
                    intent.putExtra("nikeName", mViewContentBinding.changeEdt.text.toString().trim())
                }
                setResult(RESULT_OK, intent)
                finish()
            }
        }
    }

}